package com.medical.service.impl;

import com.medical.mapper.UsageWithMapper;
import com.medical.pojo.UsageVO;
import com.medical.pojo.UsageWith;
import com.medical.service.UsageWithService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UsageWithServiceImpl implements UsageWithService {

    @Autowired
    private UsageWithMapper usageWithMapper;

    @Override
    public List<UsageWith> list(UsageVO usageVO) {
        return usageWithMapper.list(usageVO);
    }

    @Override
    public List<String> findCurrentUsage(UsageVO usageVO) {
        List<UsageWith> list = usageWithMapper.list(usageVO);
        List<String> currentUsage = list.stream().map(UsageWith::getCurrentUsage).collect(Collectors.toList());
        return currentUsage;
    }

    @Override
    public List<String> findLastYearUsage(UsageVO usageVO) {
        List<UsageWith> list = usageWithMapper.list(usageVO);
        List<String> lastYearUsage = list.stream().map(UsageWith::getLastYearUsage).collect(Collectors.toList());
        return lastYearUsage;
    }

    @Override
    public List<Integer> findWithResults(UsageVO usageVO) {
        List<String> currentUsage = findCurrentUsage(usageVO);
        List<String> lastYearUsage = findLastYearUsage(usageVO);
        List<Integer> results = calculateTongBi(currentUsage, lastYearUsage);
        return results;
    }

    public static List<Integer> calculateTongBi(List<String> currentUsage, List<String> lastYearUsage){
        List<Integer> results = new ArrayList<>();

        for (int i = 0; i < currentUsage.size(); i++) {
            int current = Integer.parseInt(currentUsage.get(i));
            int previous = Integer.parseInt(lastYearUsage.get(i));
            int with = (int)(((double)(current - previous) / previous) * 100);
            results.add(with);
        }
        return results;
    }
}
